package com.cheng.admin.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.cheng.admin.model.Invite;
import com.cheng.admin.service.InviteService;
import com.cheng.common.domain.BaseSearchVO;
import com.cheng.common.domain.R;
import com.cheng.common.utils.WeChatUtils;
import com.cheng.system.model.User;
import com.cheng.system.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * <p>
 * 用户钱包（总金额在用户表） 前端控制器
 * </p>
 *
 * @author 起凡
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/invite")
public class InviteController {
    @Autowired
    UserService userService;
    @Autowired
    InviteService inviteService;

    /**
     * 为用户生成拉新二维码
     *
     * @return
     * @throws IOException
     */
    @GetMapping("code/generate")
    public R<String> generate() throws IOException {
        User user = userService.getById(StpUtil.getLoginIdAsLong());
        if (StringUtils.isBlank(user.getQrCode())) {
            String s = WeChatUtils.generateQRCode();
            user.setQrCode(s);
            userService.updateById(user);
        }

        return R.ok(user.getQrCode());
    }


    /**
     * 发起提现申请
     *
     * @param amount 提现的金额
     * @return
     */
    @GetMapping("withdrawal")
    public R<Boolean> withdrawal(@RequestParam BigDecimal amount) {
        return R.ok(inviteService.withdrawal(amount));
    }

    /**
     * 用户A扫了B的二维码，B进入小程序调用这个接口，说明B是被A邀请的。
     *
     * @param invitorId 邀请着id，即A的id
     * @return
     * @throws Exception
     */
    @GetMapping("join")
    public R<Boolean> join(@RequestParam Long invitorId) throws Exception {
        return R.ok(inviteService.join(invitorId));
    }

    /**
     * 显示拉新记录
     *
     * @param searchVO 搜索参数
     * @return
     */
    @GetMapping("list")
    public R<PageInfo<Invite>> list(BaseSearchVO searchVO) {
        searchVO.setUserId(StpUtil.getLoginIdAsLong());
        return R.ok(inviteService.list(searchVO));
    }

    /**
     * 后台管理页面展示拉新记录
     *
     * @param searchVO 搜索参数
     * @return
     */
    @SaCheckRole("admin")
    @GetMapping("back/list")
    public R<PageInfo<Invite>> listForBack(BaseSearchVO searchVO) {
        return R.ok(inviteService.listForBack(searchVO));
    }

    /**
     * 同意提现
     *
     * @param id
     * @return
     */
    @SaCheckRole("admin")
    @GetMapping("back/check")
    public R<Boolean> check(@RequestParam Long id) {
        return R.ok(inviteService.check(id));
    }

    /**
     * 拒绝提现
     *
     * @param id
     * @param reason 拒绝理由
     * @return
     */
    @SaCheckRole("admin")
    @GetMapping("back/refuse")
    public R<Boolean> refuse(@RequestParam Long id, @RequestParam String reason) {
        return R.ok(inviteService.refuse(id, reason));
    }
}

